﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using TermitesMagnets.Targets;

namespace TermitesMagnets
{
    public class TargetRect : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private int colorIndex;
        private int targetTermiteCount;
        private int termiteCount;
        private Rectangle rectangle;
        private Color color;
        private GameplayScreen GameScreen;
        private Level parent;
        private Vector2 position;
        private Vector2 damagingPoint;
        private static Texture2D texture;
        private static int width;
        private static int height;
        public static int rectangleEdge = 48;
        private static Texture2D sqeatten;
        private double eatten;
        private ParticleSystemManager pManager;

        public int TargetTermiteCount { get { return targetTermiteCount; } set { targetTermiteCount = value; } }
        public static int Height { get { return TargetRect.height; } set { TargetRect.height = value; } }
        public static int Width { get { return TargetRect.width; } set { TargetRect.width = value; } }
        public Vector2 Position { get { return position; } set { position = value; } }
        public double Eatten { get { return eatten; } set { eatten = value; } }
        public int ColorIndex { get { return colorIndex; } set { colorIndex = value; } }
        public Color Color{ get { return color; }}
        public int TermiteCount{ get { return termiteCount; } set { termiteCount = value; }}
        public Rectangle Rectangle{ get { return rectangle; } set { rectangle = value; }}
        public Vector2 DamagingPoint { get { return damagingPoint + position; }}

        public TargetRect(Level parent, Color color, int colorIndex, int targetTermiteCount, Vector2 position, 
            Vector2 damagingPoint) : base(Game1.GlobalGame)
        {
            this.GameScreen = GameplayScreen.GlobalGame;
            this.color = color;
            this.colorIndex = colorIndex;
            this.targetTermiteCount = targetTermiteCount;
            this.parent = parent;
            this.position = position;
            this.rectangle = new Rectangle((int)position.X, (int)position.Y, rectangleEdge, rectangleEdge);
            this.parent.AddComponent(this);
            this.termiteCount = 0;
            this.eatten = 1 ;
            this.damagingPoint = damagingPoint;
            this.pManager = new ParticleSystemManager(Game1.GlobalGame, GameScreen.CurrentLevel.Board.WorldPositionToReal(Position), texture, parent);
            this.parent.AddComponent(pManager);
        }

        public float EatingSpeed()
        {
            int cover = termiteCount * 100 / TargetTermiteCount;
            return (float)cover / 10;
        }

        private int getCoverPercent()
        {
            int res = (int)eatten;
            return res;
        }

        public static void LoadContent(ContentManager Content)
        {
            texture = Content.Load<Texture2D>(@"Images\target");
            sqeatten = Content.Load<Texture2D>(@"Images\sqeatten");
            Height = texture.Height;
            Width = texture.Width;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        public bool IsTermiteOnTarget(Termite termite)
        {
            if ((this.Rectangle.Intersects(termite.GetRect())) && (termite.ColorIndex == this.colorIndex))
                return true;
            else
                return false;
        }

        public bool TargetReached()
        {
            //return termiteCount >= targetTermiteCount;
            if(Eatten >=100) 
                pManager.CurrentState = ParticleSystemManager.State.Explosions;
            return Eatten >= 100;

        }

        public override void Update(GameTime gameTime)
        {
            if (GameScreen.IsActive)
            {
                //System.Diagnostics.Debug.WriteLine("Color=" + ColorIndex + "terCount = " + termiteCount + " eatten=" + eatten);

                float timeDiff = (float)gameTime.ElapsedGameTime.TotalSeconds;
                termiteCount = 0;
                foreach (Termite termite in parent.Termites)
                {
                    if ((ColorIndex == termite.ColorIndex) && IsTermiteOnTarget(termite))
                    {
                        TermiteCount++;
                    }
                }

                Eatten += EatingSpeed() * timeDiff;
                pManager.smokePlume.maxNumParticles = (int)this.eatten / 5;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameScreen.SpriteBatch.Begin();
            Vector2 worldPosition = GameScreen.CurrentLevel.Board.WorldPositionToReal(Position);
            Vector2 textPosition = worldPosition + new Vector2(0, -20);

            //System.Diagnostics.Debug.WriteLine("Color="+ColorIndex+"terCount = " + termiteCount + " eatten=" + eatten);
            //GameScreen.SpriteBatch.DrawString(GameScreen.Font, "Time: " + GameplayScreen.seconds.ToString(), new Vector2(0, 0), Color.White);
            GameScreen.SpriteBatch.DrawString(GameScreen.Font, "% " + getCoverPercent().ToString(), textPosition, this.color);
            GameScreen.SpriteBatch.Draw(texture, worldPosition, null,
                        color, 0f, new Vector2(),
                        2f, SpriteEffects.None, 0f);           
            //GameScreen.SpriteBatch.Draw(sqeatten, new Rectangle((int)worldPosition.X, (int)worldPosition.Y, texture.Width*2, (int)Eatten) , Color.White);
            GameScreen.SpriteBatch.End();
            base.Draw(gameTime);
        }

        public void reduceatten(int reducee) 
            {
                double e = this.eatten;
                if ((e - reducee) > 0)
                {
                    e -= reducee;
                }
                else e = 0;

                this.eatten = e;
            }
        public void addeatten(int adde) 
            {
                double e = this.eatten;
                if ((e + adde) <= 100)
                {
                    e += adde;
                }
                else e = 100;

                this.eatten = e;
            }
        

    }
}
